

let mysqlModule = require("mysql");

let db = require("../config/db");

class mysql {

    /**
     * 初始化
     */
    constructor() {

    }


    query(sql, value) {

        let promise = new Promise(function (resolve, reject) {
            let connection = mysqlModule.createConnection({ host: db.host, user: db.user, password: db.password, database: db.database });

            connection.connect();
            /**
             * 推荐使用占位符方式查询，占位符可以防止 sql注入 占了 80%漏洞都是sql
             */
            connection.query(sql, value, function (err, result) {

                if (err) {
                    reject(err.message)
                } else {

                    //setTimeout(function () {//模拟数据的慢查询
                    resolve(result);
                    //},5000)

                }

                connection.end();
            })


        })
        return promise;

    }

    /**
     * 简单的数据查询
     * @param {}} condtion 
     */
     select() {

        let sql = "select * from " + this.table;
        return  this.query(sql, '')
    }


}

module.exports = mysql;